<?php

namespace app\common\logic;

use think\Db;

/**
 * 登录逻辑
 * Class SendLogic
 */
class LoginLogic extends Logic
{
    /**
     * 注册用户信息
     * @param array $param
     * @return array
     */
    public static function reg_user($param = [])
    {
        $id = Db::name('user')->where([
            'mobile' => $param['phone'],
        ])->value('id');
        if ($id > 0) return ['code' => 0, 'msg' => '手机号已注册', 'data' => $id];

        $username = 'ap' . rand_string(8, 1);
        $id = Db::name('user')->insertGetId([
            'name' => $username,
            'password' => isset($param['password']) ? md5($param['password']) : md5('rp123456'),
            'mobile' => $param['phone'],
            'last_login_ip' => get_client_ip(),
            'status' => 1,
            'add_time' => time(),
        ]);

        return ['code' => 1, 'msg' => '注册成功', 'data' => $id];
    }

    /**
     * 用户登录信息
     * @param array $param
     * @return array
     */
    public static function user_log($param = [])
    {
        $id = isset($param['id']) && $param['id'] > 0 ? $param['id'] : 0;

        if ($id == 0) {
            if (!empty($param['phone'])) {
                $id = Db::name('user')->where(['mobile' => $param['phone']])->value('id');
            }

            if (!empty($param['open_id'])) {
                $id = Db::name('user')->where(['open_id' => $param['open_id']])->value('id');
            }
        }

        if ($id > 0) {
            Db::name('user_log')->insert([
                'uid' => $id,
                'login_ip' => get_client_ip(),
                'add_time' => time(),
            ]);

            Db::name('user')->where(['id' => $id])->update([
                'last_login_time' => time(),
                'last_login_ip' => get_client_ip(),
            ]);

            $user = Db::name('user')->where(['id' => $id])
                ->field('id,org_id,name,real_name,open_id,mobile,status')->find();

            session('user_login_info', [
                'id' => $user['id'],
                'name' => $user['name'],
                'real_name' => $user['real_name'],
                'mobile' => $user['mobile'],
                'status' => $user['status'],
                'open_id' => $user['open_id'],
            ]);
        }

        return ['code' => 1, 'msg' => '记录成功'];
    }

    public static function is_login()
    {
        if (self::is_wechat()) {
            $wechat_openid = session('wechat_openid');

            if (empty($wechat_openid)) return ['code' => 0, 'msg' => '微信凭证为空'];

            $user_login_info = session('user_login_info');
            if (empty($user_login_info)) LoginLogic::user_log(['open_id' => $wechat_openid]);

            return ['code' => 1, 'msg' => '获取用户信息成功'];
        }

        if (Logic::is_debug() && !self::is_wechat()) {
            $user_login_info = session('user_login_info');
            if (empty($user_login_info)) LoginLogic::user_log(['id' => session('user_id')]);

            $user_login_info = session('user_login_info');
            session('wechat_openid', $user_login_info['open_id']);
        }

        return ['code' => 0, 'msg' => '非微信客户端登录'];
    }

    /**
     * 是否关注
     */
    public static function is_subscribe()
    {
        if (self::is_wechat()) {
            $wechat_openid = session('wechat_openid');
            $subscribe = Db::name('wechat_user')->where('open_id', $wechat_openid)->value('subscribe');

            return $subscribe ? true : false;
        }

        return true;
    }

    /**
     * 更新用户信息
     * @return array
     */
    static function refresh_user_info()
    {
        $user_login_info = session('user_login_info');
        if (empty($user_login_info)) return ['code' => 0, 'msg' => '信息未获取'];

        $user = Db::name('user')->where(['id' => $user_login_info['id']])
            ->field('id,org_id,name,real_name,open_id,mobile,status')->find();

        if (empty($user)) return ['code' => 0, 'msg' => '用户信息为空'];

        session('user_login_info', [
            'id' => $user['id'],
            'name' => $user['name'],
            'real_name' => $user['real_name'],
            'mobile' => $user['mobile'],
            'status' => $user['status'],
            'open_id' => $user['open_id'],
        ]);
    }

    /**
     * 用户登录信息
     * @param array $param
     * @return array
     */
    public static function org_user_log($param = [])
    {
        $id = isset($param['id']) && $param['id'] > 0 ? $param['id'] : 0;
        $organ_config = Db::name('organ_config');
        if ($id > 0) {
            Db::name('org_user_log')->insert([
                'uid' => $id,
                'login_ip' => get_client_ip(),
                'add_time' => time(),
            ]);

            Db::name('organization')->where(['id' => $id])->update([
                'last_login_time' => time(),
                'last_login_ip' => get_client_ip(),
            ]);

            session($param['user_auth'], [
                'id' => $param['id'],
                'user_name' => $param['user_name'],
                'name' => $param['name'],
                'thumb' => $param['thumb'],
                'address' => $param['address'],
                'mobile' => $param['mobile'],
                'intro' => $param['intro'],
            ]);

            $domain = $organ_config->where('org_id', $id)->value('domain');
            if (empty($domain)) {
                self::create_org_config($id);
            }

            if (!empty($domain)) session('domain_big_chun', $domain);
        }

        return ['code' => 1, 'msg' => '记录成功'];
    }

    protected static function create_org_config($id)
    {
        $organ_config = Db::name('organ_config');
        $system = CacheLogic::get_system();

        $rr = $system['domain_suff'] . rand_string(6, 1) . "." . $system['domain_suff'];
        $domain = $rr . ".bigchun.com";

        $org_data = [
            'domain' => $domain,
        ];
        session('domain_big_chun', $domain);
        $organ_config->insert($org_data);

        VodLogic::add_domain_record([
            'domain' => $rr,
            'org_id' => $id,
        ]);
    }

}